package com.engr.lyx.blog.biz.repository.article;

import com.engr.lyx.blog.biz.repository.BaseDao;
import com.engr.lyx.blog.client.entity.Article;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;

import java.util.List;
import java.util.Optional;

/**
 * @program: blog
 * @desc:
 * @author: lyx
 * @date: 2019-05-09 20:18
 **/
public interface ArticleRepository extends BaseDao<Article> {

    /**
     * 根据用户id查找
     *
     * @param userId userId
     * @return
     */
    Page<Article> findByUserId(long userId, Pageable pageable);

    /**
     * 根据标签id查找发表的所有文章
     *
     * @param tagId
     * @return
     */
    Page<Article> findByTagIdAndUserId(Integer tagId, Long userId, Pageable pageable);

    /**
     * 查找用户最热门文章列表
     *
     * @param userId
     */
    @Query(value = "SELECT * FROM `article` WHERE user_id = ?1 ORDER BY tags2 DESC LIMIT 3", nativeQuery = true)
    List<Article> findAllByOrderByTags2Desc(Long userId);

    /**
     * 根据id查询上篇文章
     */
    @Query(value = "SELECT * FROM article WHERE id = (SELECT id FROM article WHERE id < ?1 and user_id =?2 ORDER BY id DESC LIMIT 1)", nativeQuery = true)
    Optional<Article> findPreArticle(long id, long userId);

    /**
     * 根据id查询下篇文章
     */
    @Query(value = "SELECT * FROM article WHERE id = (SELECT id FROM article WHERE id > ?1 and user_id =?2 ORDER BY id ASC LIMIT 1)", nativeQuery = true)
    Optional<Article> findNextArticle(long id, long userId);

    /**
     * 查找归档
     * @param status 文章状态
     * @return
     */
    List<Article> findByStatusOrderByPublishDateDesc(int status);


}
